package ltd.dujiabao.orderservice.metrics;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import ltd.dujiabao.orderservice.dao.OutboxMessageRepository;
import ltd.dujiabao.orderservice.domain.enums.MessageStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @author Bowie Du
 * @date 2025/7/12 21:26
 */

@Component
public class OutboxMetrics implements MeterBinder {

    @Autowired
    private OutboxMessageRepository outboxMessageRepository;

    @Override
    public void bindTo(MeterRegistry registry) {
        for (MessageStatus messageStatus : MessageStatus.values()) {
            registry.gauge("outbox.message.count",
                    List.of(Tag.of("status", messageStatus.name())),
                    outboxMessageRepository,
                    repository -> repository.countByStatus(messageStatus.getValue()));
        }
    }
}
